package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.datas.MusicListDatas;

import java.util.List;

/**
 * Created by quyan on 15/11/3.
 */
public class PlayQueueAdapter extends BaseAdapter {
    private List<MusicListDatas> datases;
    private Context context;
    public PlayQueueAdapter(Context context,List<MusicListDatas> list) {
        this.context = context;
        datases = list;
    }

    @Override
    public int getCount() {
        return datases!= null && datases.size() > 0 ? datases.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datases.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.playqueue_item,null);
            holder = new ViewHolder();
            holder.playQueueAuthers = (TextView) convertView.findViewById(R.id.playQueueAuthers);
            holder.playQueueSongName = (TextView) convertView.findViewById(R.id.playQueueSongName);
            holder.delete = (ImageView) convertView.findViewById(R.id.delete);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.playQueueSongName.setText(datases.get(position).getName());
        holder.playQueueAuthers.setText(datases.get(position).getSinger());
        holder.delete.setTag(position);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "删除", Toast.LENGTH_SHORT).show();
               remove(position);

            }
        });

        return convertView;
    }

    public class ViewHolder{
        private TextView playQueueSongName,playQueueAuthers;
        private ImageView delete;

    }

    public void remove(int index) {
         datases.remove(index);

        notifyDataSetChanged();
    }
}
